package com.devops.admin.graphql;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.devops.admin.model.DOSProjectWebhook;
import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSWebhookInfoPo;
import com.devops.admin.service.DOSProjectWebhookService;
import com.devops.admin.service.DOSWebhookCustomInfoService;
import com.devops.admin.vo.DOSPageVo;
import com.devops.admin.vo.DOSWebhookCustomInfoVo;
import com.devops.admin.vo.DOSWebhookInfoVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author hyb22
 * @version 1.0.0
 * @description webhook操作
 * @date 2022/10/20 10:42
 */
@DgsComponent
public class DOSProjectWebhookDataFetcher {

    @Autowired
    private DOSProjectWebhookService dosProjectWebhookService;
    @Autowired
    private DOSWebhookCustomInfoService dosWebhookCustomInfoService;

    /**
     * 保存项目与webhook的关系
     * @param webhook
     * @return
     */
    @DgsMutation
    @DOSLog(description = "保存项目与webhook的关系")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public boolean saveWebhook(@InputArgument DOSProjectWebhook webhook){

        return dosProjectWebhookService.saveWebhook(webhook);
    }

    /**
     * 更新webHook
     * @param webhook
     * @return
     */
    @DgsMutation
    @DOSLog(description = "更新项目与webhook的关系")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public boolean updateWebhook(@InputArgument DOSProjectWebhook webhook){

        return dosProjectWebhookService.updateWebhook(webhook);
    }

    /**
     * @Author heyabo
     * @Description 保存webhook的配置信息
     * @Date 17:09 2022/11/23
     * @Param [webhookCustomInfo]
     * @return boolean
     **/
    @DgsMutation
    @DOSLog(description = "保存webhook的配置信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public boolean saveWebhookCustomInfo(@InputArgument DOSWebhookCustomInfoVo webhookCustomInfo){
        return dosWebhookCustomInfoService.saveWebhookCustomInfo(webhookCustomInfo);
    }

    /**
     * @Author heyabo
     * @Description 修改webhook的配置信息
     * @Date 17:14 2022/11/23
     * @Param
     * @return
     **/
    @DgsMutation
    @DOSLog(description = "修改webhook的配置信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public boolean updateWebhookCustomInfo(@InputArgument DOSWebhookCustomInfoVo webhookCustomInfo){
        return dosWebhookCustomInfoService.updateWebhookCustomInfo(webhookCustomInfo);
    }

    /**
     * 删除webHook
     * @param id
     * @return
     */
    @DgsMutation
    @DOSLog(description = "删除webHook")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public boolean deleteWebhook(@InputArgument Integer id){

        return dosProjectWebhookService.deleteWebhookById(id);
    }

    /**
     * 根据项目id查询webHook
     * @param projectId
     * @return
     */
    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public List<DOSProjectWebhook> getWebhooks(@InputArgument Integer projectId){

        return dosProjectWebhookService.list(Wrappers.<DOSProjectWebhook>lambdaQuery()
                .eq(DOSProjectWebhook::getProjectId,projectId));
    }

    /**
     * @Author heyabo
     * @Description //TODO
     * @Date 15:21 2022/11/8
     * @Param [webhook, pageVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.model.DOSProjectWebhook>
     **/
    @DgsQuery
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    @DOSLog(description = "分页查询webHook")
    public TableResult<DOSWebhookInfoPo> getWebhookPage(@InputArgument DOSProjectWebhook webhook, @InputArgument DOSPageVo pageVo){

        return dosProjectWebhookService.getWebhookPage(webhook,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 测试webhook
     * @Date 15:21 2022/11/8
     * @Param [webhook]
     * @return java.lang.String
     **/
    @DgsQuery
    @DOSLog(description = "测试webhook")
    public String testWebhook(@InputArgument DOSProjectWebhook webhook){

        return dosProjectWebhookService.testWebhook(webhook);
    }

    /**
     * @Author heyabo
     * @Description 测试webhook的配置新
     * @Date 15:12 2022/12/2
     * @Param [webhookInfoVo]
     * @return java.lang.String
     **/
    @DgsQuery
    @DOSLog(description = "测试webhook的配置信息")
    public String testWebhookCustomInfo(@InputArgument DOSWebhookInfoVo webhookInfoVo){

        return dosWebhookCustomInfoService.testWebhookCustomInfo(webhookInfoVo);
    }
}
